package com.xmu.makerspace.service;

import com.xmu.makerspace.dao.FormalMemberRepository;
import com.xmu.makerspace.dao.FormalTeamRepository;
import com.xmu.makerspace.dao.SeatRepository;
import com.xmu.makerspace.domain.FormalMember;
import com.xmu.makerspace.domain.FormalTeam;
import com.xmu.makerspace.domain.Seat;
import com.xmu.makerspace.model.SimpleSeatInfo;
import com.xmu.makerspace.model.fileprint.TeamVO;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by status200 on 2017/9/23.
 */
@Service
public class PrintFileService {

    private final Log log = LogFactory.getLog(PrintFileService.class);

    private final SeatRepository seatRepository;

    private final FormalTeamRepository formalTeamRepository;

    private final FormalMemberRepository formalMemberRepository;

    public PrintFileService(SeatRepository seatRepository, FormalTeamRepository formalTeamRepository, FormalMemberRepository formalMemberRepository) {
        this.seatRepository = seatRepository;
        this.formalTeamRepository = formalTeamRepository;
        this.formalMemberRepository = formalMemberRepository;
    }

    /**
     * 获取填充文件所需要的信息
     *
     * @param teamId
     * @return
     */
    public TeamVO getCurrentTeamInfo(int teamId) {
        FormalTeam team = formalTeamRepository.findOne(teamId);

        List<FormalMember> members = formalMemberRepository.findByTeamId(teamId);

        TeamVO vo = new TeamVO();

        vo.setTeamId(team.getTeamId());
        vo.setTeamName(team.getTeamName());

        List<SimpleSeatInfo> seats = new ArrayList<>();
        vo.setSeats(seats);

        members.forEach(e->{
            SimpleSeatInfo simpleSeatInfo = seatRepository.findSimpleSeatInfoByStudentId(e.getStudentId());

            if(simpleSeatInfo == null) {
                log.warn(String.format("student \"%s\" in team \"%s\" doesn't has seat",e.getStudentId(),team.getTeamId()));
            } else {
                seats.add(simpleSeatInfo);
            }
        });

        return vo;
    }
}
